<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\office\model\Navbar as NavbarModel;
use app\office\model\Employee as EmployeeModel;

class Index extends Controller
{
    public function index()
    {
       $this->redirect('index/index/login');
    }
    
    public function login(Request $request){
      return $this->fetch();
    }
    
    public function login_msg($data=""){
      $data=input('post.');
      // 设置Session
      if(input('?post.remember')&&(1==input('post.remember'))){
        Session::set('user_code',$data['user_code']);
        Session::set('user_password',$data['password']);
        Session::set('remember',1);
      }else{
        Session::delete('user_code');
        Session::delete('user_password');
        Session::delete('remember');
      }
      // 输入的数据通过common/login验证
      $result = $this->validate($data,'login');
      if (true !== $result) {
        return $result;
      }
      $user_code=$data['user_code'];
      $password=$data['password'];
      $result=EmployeeModel::field('id,code,password,title,address')->where('code',$user_code)->find();
      if(!$result){
        $this->error("员工编号错误,请核对",'login','',1);
      }
      $salt=substr($result['password'],-4);
      $password_in_mem=$result['password'];
      // 判断密码是否正确
      if($password_in_mem==(md5($password.$salt).$salt)){
        // 设定cookie↓
        $cookie_text=md5(md5($result['code']).md5($result['title']));
        cookie('login_code',$result['code'],65000);
        cookie('login_name',$result['title'],65000);
        cookie('cookie_text',$cookie_text,65000);
        $this->redirect('office/index/index');
      }else{
        $this->error("密码不正确,请核对",'login','',1);
      }
    }

    public function hello(){
      $data=input('post.user_code');
      $result=EmployeeModel::where('code',$data)->value('title');
      if($result){
        return $result;
      }else{
        return "请确认编号是否正确";
      }
    }
    
    public function logout(){
      cookie(null,'login_');
      cookie('cookie_text', null);
      $this->redirect('index/index/index');
    }
}
